// XMLLib version 1 - ProjectSorter example
//
// Author
//
//   Thomas Koos (info@muetze1.de)
//   www.muetze1.de
//
// License
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 1.1. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/1.1/.
//
// Revision
//
//   $Id: ProjectSorter.dpr 15 2014-04-21 21:20:02Z muetze1@gmail.com $
//

program ProjectSorter;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  SysUtils,
  SorterMain in 'SorterMain.pas',
  SorterConfig in 'SorterConfig.pas',
  StrAdapter in '..\..\library\StrAdapter.pas',
  StrAdapterBase in '..\..\library\StrAdapterBase.pas',
  StrAdapterDefault in '..\..\library\StrAdapterDefault.pas',
  StrAdapterEntity in '..\..\library\StrAdapterEntity.pas',
  StrAdapterISO8859_1 in '..\..\library\StrAdapterISO8859_1.pas',
  StrAdapterOptions in '..\..\library\StrAdapterOptions.pas',
  StrAdapterUTF_8 in '..\..\library\StrAdapterUTF_8.pas',
  StrAdapterWindows1252 in '..\..\library\StrAdapterWindows1252.pas',
  StrMasks in '..\..\library\StrMasks.pas',
  XMLLib in '..\..\library\XMLLib.pas',
  EnhancedStreams in '..\..\external\EnhancedStreams.pas',
  UnicodeFileUtils in '..\..\external\UnicodeFileUtils.pas';

  // print out the usage of this tool
procedure ShowUsage;
begin
  WriteLn('  usage: ProjectSorter <projectfile> [<projectfile> ...]');
  WriteLn;
  WriteLn('Intended to be used on Embarcadero XML project files using MSBuild');
end;

procedure Main;
var
  lConfig: TSorterConfig;
  i: Integer;
  lFilename: string;
begin
  WriteLn('ProjectSorter - sorting project files for better diff handling');
  WriteLn('Done in 2014 by Muetze1. Part of the XMLLib version 1');
  WriteLn;

  if 0 < ParamCount then
  begin
    lConfig := TSorterConfig.Create;
    try
      lFilename := ChangeFileExt(ParamStr(0), '.xml');

      if lConfig.LoadFromFile(lFilename) then
        WriteLn('configuration file ', lFilename, ' loaded');

      for i := 1 to ParamCount do
      begin
        lFilename := ParamStr(i);

        Write('processing ', lFilename, ' ...', #13);

        if SortProject(lFilename, lConfig) then
          WriteLn('project ', lFilename, ' sorted')
        else
          WriteLn('failed to process file ', lFilename);
      end;
    finally
      lConfig.Free;
    end;
  end
  else
    ShowUsage();
end;

begin
  try
    Main();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
